package com.cloud.template.base.util;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 压缩工具
 * @author zhenghao
 *
 */

public class ZipUtil {
	private static final Logger LOGGER= Logger.getLogger(ZipUtil.class);
	
	/**
	 *  一次性压缩多个文件，文件存放至一个文件夹中
	 *  filepath 被压缩文件目录
	 *  zippath 压缩文件目录
	 **/
    public static void pressZipFile(String filepath , String zippath) {
    	LOGGER.info("压缩文件开始，压缩文件："+filepath+"，压缩包："+zippath);
		ZipOutputStream zipOut = null;
		try {
	        File file = new File(filepath);// 要被压缩的文件夹
	        File zipFile = new File(zippath);
	        zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
			pressZip(file.getName(),file,zipOut);
	        LOGGER.info("压缩文件结束，压缩文件："+filepath+"，压缩包："+zippath);
		} catch (Exception e) {
			LOGGER.error("压缩文件失败，压缩文件："+filepath+"，压缩包："+zippath,e);
		}finally {
			if (zipOut!=null){
				try {
					zipOut.close();
				} catch (IOException e) {
					LOGGER.error(e);
				}
			}
		}
	}

	//递归全部文件
	private static void pressZip(String zippath, File file , ZipOutputStream zipOut){
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(int i = 0; i < files.length; ++i){
				pressZip(zippath + File.separator + files[i].getName(),files[i],zipOut);
			}
		}else {
			pressInZipStream(zippath==null?file.getName():zippath,file ,zipOut);
		}
	}

	//将文件放入压缩流中
	private static void pressInZipStream(String zippath, File file , ZipOutputStream zipOut){
		FileInputStream in = null;
		BufferedInputStream bis = null;
		try{
			in = new FileInputStream(file);
			bis = new BufferedInputStream(in);
			zipOut.putNextEntry(new ZipEntry(zippath));
			int buffer = 10240;
			byte[] data = new byte[buffer];
			while(bis.read(data,0,buffer) != -1){
				zipOut.write(data,0,buffer);
			}
		}catch (Exception e){
			LOGGER.error(e);
		}finally {
			if (in != null){
				try {
					in.close();
				} catch (IOException e) {
					LOGGER.error(e);
				}
			}
			if (bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					LOGGER.error(e);
				}
			}
		}
	}
	
	/**
	 *   解压缩
	 *   压缩文件中包含一个或多个文件
     * */
    public static void untieZipFile(String zippath , String outzippath){
    	LOGGER.info("压缩文件开始，压缩包："+zippath+"，解压文件："+outzippath);
    	try {
            File file = new File(zippath);
            File outFile = null;
            ZipFile zipFile = new ZipFile(file);
            ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            InputStream input = null;
            OutputStream output = null;
            while((entry = zipInput.getNextEntry()) != null){
                System.out.println("解压缩" + entry.getName() + "文件");
                outFile = new File(outzippath + File.separator + entry.getName());
                if(!outFile.getParentFile().exists()){
                    outFile.getParentFile().mkdir();
                }
                if(!outFile.exists()){
                    outFile.createNewFile();
                }
                input = zipFile.getInputStream(entry);
                output = new FileOutputStream(outFile);
                int temp = 0;
                while((temp = input.read()) != -1){
                    output.write(temp);
                }
                input.close();
                output.close();
            }
            zipInput.close();
            zipFile.close();
            LOGGER.info("压缩文件结束，压缩包："+zippath+"，解压文件："+outzippath);
		} catch (Exception e) {
			LOGGER.error("解压文件失败，压缩包："+zippath+"，解压文件："+outzippath,e);
		}
    }
}
